class Solution:
    def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
        stack = []
        
        for num in nums:
            current = num
            # 不断与栈顶元素检查并合并
            while stack:
                gcd_val = math.gcd(current, stack[-1])
                if gcd_val > 1:
                    # 计算LCM并合并
                    current = current * stack.pop() // gcd_val
                else:
                    break
            stack.append(current)
            
        return stack

        